# read and write json file
# -*- coding:utf-8 -*-
import json

nums_table_data_dict = {}
pre2NumsStrList = []
buyTimesListTemp = {}
moneyListTemp = {}
costList = {}
idList = {}


def get_default_row_values(pre2_nums_str, ids):
    row_value = []
    row_value.append(ids)
    row_value.append(pre2_nums_str)
    row_value.append('True')
    row_value.append(0)
    row_value.append(-1)
    row_value.append(1)
    row_value.append(-1)
    row_value.append(-1)
    row_value.append(0.00)
    return row_value


def init_default_value():
    count = 0
    init_pre2_nums_list()
    tableDataList = {}
    if len(pre2NumsStrList) == 45:
        while count < 45:
            tableData = {}
            nums_id = count + 1
            tableData["num_id"] = nums_id
            tableData["buy_times"] = 0
            tableData["money"] = 0
            tableData["status"] = 0
            tableData["num_value"] = pre2NumsStrList[count]
            tableData["is_selected"] = 0
            tableData["miss_count"] = 0
            tableData["miss_count_week"] = 0
            tableData["miss_count_max"] = 0
            tableDataList[count] = tableData
            # row_values = get_default_row_values(pre2NumsStrList[count], nums_id)
            # tableData.append(row_values)
            count = count + 1

    nums_table_data_dict['nums_table_data_dict'] = tableDataList
    column_list = {}
    column_labels_list = []
    column_labels_list.append("ID")
    column_labels_list.append(u"组合")
    column_labels_list.append(u"选择")
    column_labels_list.append(u"手数")
    column_labels_list.append(u"空期")
    column_labels_list.append(u"投注状态")
    column_labels_list.append(u"五日空期")
    column_labels_list.append(u"最大空期")
    column_labels_list.append(u"当前金额")
    column_key_list = []
    column_key_list.append("num_id")
    column_key_list.append("num_value")
    column_key_list.append("is_selected")
    column_key_list.append("buy_times")
    column_key_list.append("miss_count")
    column_key_list.append("status")
    column_key_list.append("miss_count_week")
    column_key_list.append("miss_count_max")
    column_key_list.append("money")
    column_read_only_list = []
    column_read_only_list.append(1)  # 空字符串自动转为FALSE
    column_read_only_list.append(1)
    column_read_only_list.append(0)
    column_read_only_list.append(0)
    column_read_only_list.append(1)
    column_read_only_list.append(1)
    column_read_only_list.append(1)
    column_read_only_list.append(1)
    column_read_only_list.append(1)
    for i in range(len(column_labels_list)):
        dict2 = {}
        dict2['key'] = column_key_list[i]
        # dict2['column_index'] = i
        dict2['label_value'] = column_labels_list[i]
        dict2['is_read_only'] = column_read_only_list[i]
        column_list[i] = dict2

    nums_table_data_dict['nums_area_column_dict'] = column_list
    return nums_table_data_dict


def init_pre2_nums_list():
    sum_three = 3  #
    sum_nineteen = 19  #
    for sum_value in range(sum_three, sum_nineteen + 1):
        half_sum_value = sum_value / 2
        for j in range(1, half_sum_value + 1):
            k = sum_value - j
            if k == j or j > 10 or k > 10:
                continue
            if j == 10:
                j = 0
            if k == 10:
                k = 0
            pre2_num_str = str(j) + str(k) + '  ' + str(k) + str(j)
            pre2NumsStrList.append(pre2_num_str)
    pass


new_dict1 = init_default_value()
# print(test_dict)
# print(type(test_dict))

# dumps the str to json
json_str = json.dumps(new_dict1)
# print(json_str)
# print(type(json_str))

# load the json from str
new_dict = json.loads(json_str)
# print(new_dict)
# print(type(new_dict))

# write the json to a file
with open("../learn_json/record.json", "w") as f:
    json.dump(new_dict, f)
print("write to file success...")

# read the json from the file
with open("../learn_json/record.json", 'r') as load_f:
    load_dict = json.load(load_f)
    print(load_dict)

nums_area_column_labels = load_dict["nums_area_column_dict"]
print nums_area_column_labels
print(type(nums_area_column_labels))
# load_dict['smallberg'] = [8200, {1: [['Python', 81], ['shirt', 300]]}]
# print(load_dict)
#
# with open("../learn_json/record.json", "w") as dump_f:
#     json.dump(load_dict, dump_f)
